﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Bussiness.Entity;
using Bussiness.Manager;
namespace DSC
{
    public partial class ucActionHistory : AfUserControl
    {
        public ucActionHistory()
        {
            InitializeComponent();
            dtpFrom.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
            dtpTo.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            ShowStatusBar("Đang tìm kiếm");
            DateTime from = dtpFrom.Value;
            DateTime to = dtpTo.Value;
            if (to.Subtract(from).TotalDays > 90)
            {
                MessageBox.Show("Vui lòng chỉ xem dữ liệu trong vòng 3 tháng");
                return;
            }
            DataTable dt = PT_ActionHistoryManager.CreateInstant().search(from, to, txtSoPhieu.Text);
            DataTable newdt = new DataTable();
            newdt.Columns.Add("ActionDate");
            newdt.Columns.Add("User");
            newdt.Columns.Add("ActionName");
            newdt.Columns.Add("Description");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = newdt.NewRow();
                dr["ActionDate"] = ((DateTime)dt.Rows[i]["ActionDate"]).ToString("dd/MM/yyyy hh:mm:ss");
                dr["User"] = dt.Rows[i]["UserCode"].ToString() + "_" + dt.Rows[i]["UserName"].ToString();
                dr["ActionName"] = dt.Rows[i]["ActionName"].ToString();
                dr["Description"] = dt.Rows[i]["Description"].ToString();
                newdt.Rows.Add(dr);
            }
            gridHistory.DataSource = newdt;
            EndShowStatusBar();
        }

        private void gridHistory_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > 0 && e.RowIndex > -1)
            {
                string nguoidung = gridHistory["User", e.RowIndex].Value.ToString();
                string ngay = gridHistory["ActionDate", e.RowIndex].Value.ToString();
                string hanhdong = gridHistory["ActionName", e.RowIndex].Value.ToString();
                string noidung = gridHistory["Description", e.RowIndex].Value.ToString();
                frmActionHistory frm = new frmActionHistory(nguoidung, ngay, hanhdong, noidung);
                frm.ShowDialog();
            }
        }
    }
}
